extends Node
class_name AIBehavior

const DURATION_AI_TICK_FREQUENCY := 200

var ball : Ball = null
var opponent_detection_area : Area2D = null
var teammate_detection_area: Area2D =null
var player : Player = null
var time_since_last_ai_tick := Time.get_ticks_msec()

func _ready() -> void:
	time_since_last_ai_tick = Time.get_ticks_msec() + randi_range(0, DURATION_AI_TICK_FREQUENCY)

func setup(	context_player: Player,
			context_ball: Ball,
			contet_opponent_detection_area: Area2D,
			contet_teammate_detection_area: Area2D,
			) -> void:
	player = context_player
	ball = context_ball
	opponent_detection_area = contet_opponent_detection_area
	teammate_detection_area = contet_teammate_detection_area

func process_ai() -> void:
	if Time.get_ticks_msec() - time_since_last_ai_tick > DURATION_AI_TICK_FREQUENCY:
		time_since_last_ai_tick = Time.get_ticks_msec()
		perform_ai_movement()
		perform_ai_decisions()

func perform_ai_movement() -> void:
	pass
func perform_ai_decisions() -> void:
	pass

func get_bicircular_weight (
		position: Vector2,
		center_target: Vector2,
		inner_circle_radius: float,
		inner_circle_weight: float,
		outer_circle_radius: float,
		outer_circle_weight: float,
	) -> float:
		var distance_to_center := position.distance_to(center_target)
		if distance_to_center > outer_circle_radius:
			return outer_circle_weight
		elif distance_to_center < inner_circle_radius:
			return inner_circle_weight
		else:
			var distance_to_inner_radius := distance_to_center - inner_circle_radius
			var close_range_distance := outer_circle_radius - inner_circle_radius
			return lerpf(inner_circle_weight, outer_circle_weight, distance_to_inner_radius / close_range_distance)

func face_toward_target_goal() -> void:
	if not player.is_facing_target_goal():
		player.heading *= -1

func is_ball_possessed_by_oppenent() -> bool:
	return ball.carrier != null and ball.carrier.country != player.country

func is_ball_carried_by_teammate() -> bool:
	return ball.carrier != null and ball.carrier != player and ball.carrier.country == player.country

func has_opponents_nearby() -> bool:
	var players := opponent_detection_area.get_overlapping_bodies()
	return players.find_custom(func(p: Player): return p.country != player.country) > -1
